package org.tacademy.basic.calendar.databasemanager;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.tacademy.basic.calendar.ItemData;
import org.tacademy.basic.calendar.MyApplication;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;

public class DBManager {

	private static DBManager instance;
	
	private MyDatabaseOpenHelper mDBHelper;
	
	SimpleCursorAdapter mAdapter;
	//Cursor mCursor = null;
	
	Handler mHandler = new Handler();
	
	public static DBManager getInstance() {
		if (instance == null) {
			instance = new DBManager();
		}
		return instance;
	}
	
	private DBManager() {
		mDBHelper = new MyDatabaseOpenHelper(MyApplication.getContext(), null, null, 0);
	}
	
	
	public void insertSchedule(ItemData data) {
		ContentValues values = new ContentValues();
		values.put(DBConstant.ScheduleTable.TITLE, data.title);
		values.put(DBConstant.ScheduleTable.CONTENT, data.content);
		values.put(DBConstant.ScheduleTable.DATE, data.year+"-"+(data.month+1)+"-"+data.day);
		values.put(DBConstant.ScheduleTable.TIME, data.time);
		values.put(DBConstant.ScheduleTable.LOCATION, data.location);
		
		SQLiteDatabase db = mDBHelper.getWritableDatabase();
		long d = db.insert(DBConstant.ScheduleTable.TABLE_NAME, null, values);
		db.close();
	}
	

	public List<ItemData> getListSchedule(){
		String[] columns = {DBConstant.ScheduleTable.ID, DBConstant.ScheduleTable.TITLE, DBConstant.ScheduleTable.DATE,
							DBConstant.ScheduleTable.TIME, DBConstant.ScheduleTable.CONTENT, DBConstant.ScheduleTable.LOCATION};
		String selection = DBConstant.ScheduleTable.DATE +" = ?";
		SQLiteDatabase db = mDBHelper.getReadableDatabase();
		
		Cursor mCursor = db.query(DBConstant.ScheduleTable.TABLE_NAME, 
				columns, null, null, null, null, null);
		
//		ScheduleRequest request = new ScheduleRequest();
//
//		DBManager.getInstance().getAsyncDBProcess(request, new DBRequest.OnQueryCompleteListener() {
//			
//			@Override
//			public void onQueryCompleted(DBRequest request) {
//				// TODO Auto-generated method stub
//				
//				Cursor c = (Cursor)request.getResult();
//				
//				if (mCursor != null) {
//					mCursor.close();
//					mCursor = null;
//				}
//				
//				mCursor = c;
//				
//				mAdapter.swapCursor(mCursor);
//			}
//		}, mHandler);
		
		List<ItemData> list = new ArrayList<ItemData>();
		int titleIndex = mCursor.getColumnIndex(DBConstant.ScheduleTable.TITLE);
		int dateIndex = mCursor.getColumnIndex(DBConstant.ScheduleTable.DATE);
		int timeIndex = mCursor.getColumnIndex(DBConstant.ScheduleTable.TIME);
		int contentIndex = mCursor.getColumnIndex(DBConstant.ScheduleTable.CONTENT);
		int locationIndex = mCursor.getColumnIndex(DBConstant.ScheduleTable.LOCATION);
		
		while(mCursor.moveToNext()){
			ItemData data = new ItemData();
			data.title = mCursor.getString(titleIndex);
			data.content = mCursor.getString(contentIndex);
			data.time = mCursor.getString(timeIndex);
			String location = mCursor.getString(locationIndex);
//			data.location = mCursor.getString(locationIndex);
			String str = mCursor.getString(dateIndex);
			StringTokenizer st = new StringTokenizer(str,"-");
			String temp = "";
			int count=0;
			while(st.hasMoreElements()){
				temp = st.nextToken();
				if(count==0){
					data.year = Integer.parseInt(temp);
					count++;
				}else if(count==1){
					data.month = Integer.parseInt(temp)-1;
					count++;
				}else if(count==2){
					data.day = Integer.parseInt(temp);
					count++;
				}	
			}
			list.add(data);
		}
		
		return list;
	}

	public void getAsyncDBProcess(DBRequest request, DBRequest.OnQueryCompleteListener listener, Handler handler) {
		request.setOnQueryCompleteListener(listener);
		request.setHandler(handler);
		new Thread(new ProcessRunnable(request)).start();
	}
	
	class ProcessRunnable implements Runnable {
		DBRequest request;
		
		public ProcessRunnable(DBRequest request) {
			this.request = request;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			SQLiteDatabase db = mDBHelper.getReadableDatabase();
			request.process(db);
		}
		
	}
	
	
}
